package com.alibaba.cirtus.ots.application.adapter.ability.utils;

import com.epoch.app.otsapplicationadaptersdk.contants.OtsApplicationAdapterSdkConstants;
import lombok.extern.slf4j.Slf4j;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author baizhuo
 * @version 1.0
 * @date 2023/12/25 22:19
 */
@Slf4j
public class HttpTokenUtil {

    public static Map<String, String> generateHttpHeader() {
        Date date = new Date();
        String timestamp = String.valueOf(date.getTime());
        String ak = OtsApplicationAdapterSdkConstants.epoch_access_key();
        String sk = OtsApplicationAdapterSdkConstants.epoch_secret_key();
        String src = timestamp + ak + sk;
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        md5.update(src.getBytes(StandardCharsets.UTF_8));
        byte[] digest = md5.digest();
        String token = DatatypeConverter.printHexBinary(digest).toLowerCase();
        Map<String, String> result = new HashMap<>();
        result.put("ak", ak);
        result.put("timestamp", timestamp);
        result.put("token", token);
        return result;
    }
}
